package cn.lili.modules.order.cart.entity.dto;

import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.security.AuthUser;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.member.entity.dos.UserAddress;
import cn.lili.modules.order.cart.entity.enums.CartSceneEnum;
import cn.lili.modules.order.cart.entity.enums.SuperpositionPromotionEnum;
import cn.lili.modules.order.cart.entity.vo.CartSkuVO;
import cn.lili.modules.order.cart.entity.vo.CartVO;
import cn.lili.modules.order.cart.entity.vo.PriceDetailVO;
import cn.lili.modules.order.order.entity.dto.PriceDetailDTO;
import cn.lili.modules.order.order.entity.vo.OrderVO;
import cn.lili.modules.order.order.entity.vo.ReceiptVO;
import cn.lili.modules.promotion.entity.dos.MemberCoupon;
import cn.lili.modules.promotion.entity.dos.ReturnActivityUser;
import cn.lili.modules.promotion.entity.vos.MemberCouponVO;
import cn.lili.modules.promotion.entity.vos.TicketAddVO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 购物车视图
 *
 * @author Chopper
 * @since 2020-03-25 2:30 下午
 */
@Data
@NoArgsConstructor
public class TradeDTO implements Serializable {

    private static final long serialVersionUID = -3137165707807057810L;

    @ApiModelProperty(value = "sn")
    private String sn;

    @ApiModelProperty(value = "是否为其他订单下的订单，如果是则为依赖订单的sn，否则为空")
    private String parentOrderSn;

    @ApiModelProperty(value = "购物车列表")
    private List<CartVO> cartList;

    @ApiModelProperty(value = "整笔交易中所有的规格商品")
    private List<CartSkuVO> skuList;

    @ApiModelProperty(value = "购物车车计算后的总价")
    private PriceDetailVO priceDetailVO;

    @ApiModelProperty(value = "购物车车计算后的总价")
    private PriceDetailDTO priceDetailDTO;

    @ApiModelProperty(value = "发票信息")
    private ReceiptVO receiptVO;

    @ApiModelProperty(value = "是否需要发票")
    private Boolean needReceipt;


    @ApiModelProperty(value = "不支持配送方式")
    private List<CartSkuVO> notSupportFreight;

    /**
     * 购物车类型
     */
    private CartSceneEnum cartSceneEnum;
    /**
     * 店铺备注
     */
    private List<StoreRemarkDTO> storeRemark;

    /**
     * sku促销连线 包含满优惠
     * <p>
     * KEY值为 sku_id+"_"+SuperpositionPromotionEnum
     * VALUE值为 对应的活动ID
     *
     * @see SuperpositionPromotionEnum
     */
    private Map<String, String> skuPromotionDetail;

    /**
     * 使用平台优惠券，一笔订单只能使用一个平台优惠券
     */
    private MemberCouponDTO platformCoupon;

    /**
     * key 为商家id
     * value 为商家优惠券
     * 店铺优惠券
     */
    private Map<String, MemberCouponDTO> storeCoupons;

    /**
     * 可用优惠券列表
     */
    private List<MemberCoupon> canUseCoupons;

    /**
     * 可用全返卷列表
     */
    private List<ReturnActivityUser> returnActivityUserList;

    /**
     * 无法使用优惠券无法使用的原因
     */
    private List<MemberCouponVO> cantUseCoupons;

    /**
     * 收货地址
     */
    private UserAddress userAddress;

    /**
     * 客户端类型
     */
    private String clientType;

    /**
     * 买家名称
     */
    private String memberName;

    /**
     * 买家id
     */
    private String memberId;

    /**
     * 分销商id
     */
    private String distributionId;

    /**
     * 分销商名称
     */
    private String distributionName;

    /**
     * 直推店主用户ID
     * */
    private String lowerCommissId;

    /**
     * 当前用户身份
     */
    private Boolean isDistribution;

    /**
     * 间推店主用户ID
     * */
    private String lowerSubCommissId;
    /**
     * 订单vo
     */
    private List<OrderVO> orderVO;

    /**
     * 抵扣F分金额
     */
    private Double fmoney = 0D;
    /**
     * 是否使用F分抵扣
     */
    private boolean useFmoney = false;

    /**
     * 飞语免密支付的key
     */
    private String freePayKey;

    private Boolean isVip = false;

    @ApiModelProperty("全返卷")
    private ReturnActivityUser returnActivityUser;

    /**
     * @see PromotionTypeEnum
     */
    @ApiModelProperty(value = "参与促销枚举 如不设置，且存在拼团、积分、砍价以外的活动，则会自动参与一项（顺序以map中促销的优先级为准）")
    private PromotionTypeEnum promotionType;

    @ApiModelProperty("套餐商品规格列表")
    private List<GoodsSku> mealSkuList;

    @ApiModelProperty("套餐商品购买渠道：0结算页面选择套餐商品、1普通渠道购买")
    private Integer mealGoodsSource;

    @ApiModelProperty("选座座位列表")
    private List<TicketAddVO> ticketAddVOList;

    @ApiModelProperty("是否有门票商品")
    private Boolean isTicket = false;

    @ApiModelProperty("全返券抵扣金额")
    private Double returnActivityPrice;

    @ApiModelProperty("商品最高可抵扣金额")
    private Double goodsMaxReturnActivityPrice;

    /**
     * 不可用全返卷列表
     */
    private List<ReturnActivityUser> cantReturnActivityUserList;

    @ApiModelProperty("买家实名用户姓名")
    private String authName;

    @ApiModelProperty("买家实名用户证件号-前端显示")
    private String authCardNo;

    @ApiModelProperty("买家实名用户证件号-后端存储")
    @JsonIgnore
    private String buyerCardNo;

    @ApiModelProperty("免税单笔交易限额")
    private Double freeTaxOnceMax = 0.0;

    @ApiModelProperty("免税年度交易限额")
    private Double freeTaxYearMax = 0.0;

    @ApiModelProperty("用户年度累记交易额度")
    private Double freeTaxUserTotalAmount = 0.0;

    private String sessionId;

    public TradeDTO(CartSceneEnum cartSceneEnum) {
        this.cartSceneEnum = cartSceneEnum;

        this.skuList = new ArrayList<>();
        this.cartList = new ArrayList<>();
        this.skuPromotionDetail = new HashMap<>();
        this.storeCoupons = new HashMap<>();
        this.priceDetailDTO = new PriceDetailDTO();
        this.cantUseCoupons = new ArrayList<>();
        this.canUseCoupons = new ArrayList<>();
        this.needReceipt = false;
        this.mealSkuList = new ArrayList<>();
        this.ticketAddVOList = new ArrayList<>();
    }

    public TradeDTO(CartSceneEnum cartSceneEnum, AuthUser currentUser) {
        this.memberId = currentUser.getExtendId();
        this.memberName = currentUser.getNickName();

        this.cartSceneEnum = cartSceneEnum;
        this.skuList = new ArrayList<>();
        this.cartList = new ArrayList<>();
        this.skuPromotionDetail = new HashMap<>();
        this.storeCoupons = new HashMap<>();
        this.priceDetailDTO = new PriceDetailDTO();
        this.cantUseCoupons = new ArrayList<>();
        this.canUseCoupons = new ArrayList<>();
        this.needReceipt = false;
        this.mealSkuList = new ArrayList<>();
        this.ticketAddVOList = new ArrayList<>();
    }

    /**
     * 过滤购物车中已选择的sku
     *
     * @return
     */
    public List<CartSkuVO> getCheckedSkuList() {
        if (skuList != null && !skuList.isEmpty()) {
            return skuList.stream().filter(CartSkuVO::getChecked).collect(Collectors.toList());
        }
        return skuList;
    }

    public void removeCoupon() {
        this.canUseCoupons = new ArrayList<>();
        this.cantUseCoupons = new ArrayList<>();
    }

}
